package com.living.player.db;

import android.text.TextUtils;

import com.md.basedpc.persistence.PHDbUtils;
import com.md.network.api.Audio;

import java.util.ArrayList;
import java.util.List;

public class AudioPlayListManger {

    private static AudioPlayListManger instance = null;

    public static AudioPlayListManger getInstance() {
        if (instance == null) instance = new AudioPlayListManger();
        return instance;
    }

    /**
     * @param albumId
     */
    public void saveAudioList(String albumId, List<Audio> audio) {
        deleteAllAudio();
        for (Audio audio1 : audio) {
            AudioPlayListDB audioHistoryDB = new AudioPlayListDB();
            audioHistoryDB.setAlbum_id(albumId);
            audioHistoryDB.setAlbum_name(audio1.getAlbumName());
            audioHistoryDB.setId(audio1.getId());
            audioHistoryDB.setName(audio1.getName());
            audioHistoryDB.setAuthor(audio1.getAuthor());
            audioHistoryDB.setCover(audio1.getCoverUrl());
            audioHistoryDB.setDesc(audio1.getDesc());
            audioHistoryDB.setUrl(audio1.getAudioUrl());
            saveAudio(albumId, audioHistoryDB);
        }
    }

    /**
     * 保存 音频信息
     *
     * @param audioDB
     */
    private AudioPlayListDB saveAudio(String albumId, AudioPlayListDB audioDB) {
        audioDB.setAlbum_id(albumId);
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(audioDB.getId()))
            where += " and id='" + audioDB.getId() + "'";
//        if (!TextUtils.isEmpty(albumId))
//            where += " and album_id=" + albumId + "";
        try {
            List<AudioPlayListDB> dbs = PHDbUtils.getInstance().query(AudioPlayListDB.class, where);
            if (dbs == null || dbs.size() == 0) {
                PHDbUtils.getInstance().insert(audioDB);
            } else {
                return dbs.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除 音频信息
     *
     * @param audioId
     * @return
     */
    public boolean deleteAudio(String audioId) {
        boolean isDelSucess = false;
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(audioId))
            where += " and id='" + audioId + "'";
        try {
            isDelSucess = PHDbUtils.getInstance().delete(AudioPlayListDB.class, where);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isDelSucess;
    }

    public boolean deleteAllAudio() {
        boolean isDelSucess = false;
        String where = " 1=1 ";
        try {
            isDelSucess = PHDbUtils.getInstance().delete(AudioPlayListDB.class, where);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isDelSucess;
    }

    /**
     * 读取所有下载数据
     *
     * @return
     */
    public List<Audio> getAllRecentAudios() {
        final List<Audio> list = new ArrayList<>();
        String where = " 1=1 ";
        where += " order by id asc ";
        try {
            List<AudioPlayListDB> dbs = PHDbUtils.getInstance().query(AudioPlayListDB.class, where);
            if (dbs == null || dbs.size() == 0) {
            } else {
                for (AudioPlayListDB audioDB : dbs) {
                    Audio model = new Audio(audioDB.getId(),
                            audioDB.getAuthor(),
                            audioDB.getCover(),
                            audioDB.getDesc(),
                            false,
                            audioDB.getName(),
                            audioDB.getUrl());
                    model.setAlbumId(audioDB.album_id);
                    model.setAlbumName(audioDB.album_name);
                    list.add(model);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


}
